package com.redwall.threads;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 可以设置定时线程池去刷新配置项
 */
public class ScheduleRefresh {

    private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);

    private static ScheduleRefresh instance = new ScheduleRefresh();


    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Properties properties = new Properties();

    private ScheduleRefresh() {
        init();
    }

    public static ScheduleRefresh getInstance() {
        return instance;
    }

    private void init() {
        executor.scheduleWithFixedDelay(() -> properties.setProperty("time", df.format(new Date())), 0, 1L, TimeUnit.SECONDS);
    }

    public String getProperties(String k){
        return properties.getProperty(k);
    }

}
